#!/bin/bash

stopuwsgi()
{
    echo "关闭uwsgi..."
    for pid in `ps -aux|grep uwsgi|grep ini|grep -v grep|awk '{print $2}'` ; do
        kill -9 ${pid}
    done
    sleep 3
    echo "已关闭!"
}
startuwsgi()
{
    if [ `ps -aux|grep uwsgi|grep ini|grep -v grep|wc -l` -ne 0 ]; then
        echo 'ERR!进程已存在，未开启！'
        return
    fi
    echo '开启uwsgi...'
    cd /app/ds_website
    uwsgi --ini uwsgi.ini
    sleep 3
    echo "uwsgi进程数：`ps -aux|grep uwsgi|grep ini|grep -v grep|wc -l`"
    echo '已开启！'
}
restartuwsgi()
{
    stopuwsgi
    startuwsgi
    echo '已重启！'
}

menu()
{
    while [ "1" ]
    do
        clear
        echo "功能菜单:"
        echo "1、重启uwsgi"
        echo "2、启动uwsgi"
        echo "3、停止uwsgi"
        echo "0、退出"
        echo "select==>>"
        tput cup 5 10
        read choice
        case ${choice} in
        0) break;;
        1) echo "重启uwsgi(y/n)"
            read key
            if [ ${key} = 'y' -o ${key} = 'Y' ]; then
                restartuwsgi
            fi
            echo '按任意键继续...'
            read key
            continue;;
        2) echo "启动uwsgi(y/n)"
            read key
            if [ ${key} = 'y' -o ${key} = 'Y' ]; then
                startuwsgi
            fi
            echo '按任意键继续...'
            read key
            continue;;
        3) echo "停止uwsgi(y/n)"
            read key
            if [ ${key} = 'y' -o ${key} = 'Y' ]; then
                stopuwsgi
            fi
            echo '按任意键继续...'
            read key
            continue;;
        esac
    done
}

if [ $1 = 'start' ]; then
    startuwsgi
elif [ $1 = 'restart' ]; then
    restartuwsgi
elif [ $1 = 'stop' ]; then
    stopuwsgi
else
    menu
fi